//
//  LGSSelectNodeTypeSheetController.m
//  LogicSim
//
//  Created by Jeremy Pereira on 29/04/2012.
//  Copyright (c) 2013 Jeremy Pereira. All rights reserved.
//

#import "LGSInputMappingSheetController.h"
#import "LGSInputMappingSelectorDelegate.h"
#import "LGSName.h"

enum  
{
    SHEET_END_OK = 0,
    SHEET_END_CANCEL = 1,
};

@interface LGSInputMappingSheetController()

@property (weak) id<LGSInputMappingSelectorDelegate> delegate;

-(void) populateConnections: (NSPopUpButton*) connections
            forNodeSelector: (NSPopUpButton*) nodeSelector;

@end

@implementation LGSInputMappingSheetController

@synthesize  destNodeSelector;

-(void) setUpUI
{
    [[self destNodeSelector] removeAllItems];
    [[self sourceInputSelector] removeAllItems];
    NSArray* sourceInputNames = [[self delegate] moduleInputNamesForSelector: self];
    [[self sourceInputSelector] addItemsWithTitles: sourceInputNames];
    for (int i = 0 ; i < [[self delegate] itemCountForNodeSelectorController: self] ; ++i)
    {
        LGSName* theNodeLabel = [[self delegate] nameForNodeController: self
                                                       		   atIndex: i];
        [[self destNodeSelector] addItemWithTitle: [theNodeLabel stringValue]];
    }
    [self populateConnections: [self destConnectionSelector]
              forNodeSelector: [self destNodeSelector]];
}

-(void) populateConnections: (NSPopUpButton*) connections
            forNodeSelector: (NSPopUpButton*) nodeSelector
{
    [connections removeAllItems];
    NSInteger selectedIndex = [nodeSelector indexOfSelectedItem];
    if (selectedIndex >= 0)
    {
        NSArray* itemNames = [[self delegate] inputNamesForNodeSelectorController: self
                                   		                        		  atIndex: selectedIndex];
        [connections addItemsWithTitles: itemNames];
    }
    
}


-(void) informDelegateOfEndSheet
{
    [[self delegate] nodeSelector: self
                selectedDestIndex: [[self destNodeSelector] indexOfSelectedItem]
                    selectedInput: [[self sourceInputSelector] titleOfSelectedItem]
                selectedDestInput: [[self destConnectionSelector] titleOfSelectedItem]];
}

-(IBAction) changeNodeSelection: (id)sender
{
    [self populateConnections: [self destConnectionSelector]
              forNodeSelector: [self destNodeSelector]];
}


@end
